import { ADD_CART, EDIT_CART } from '../constent'
const initcartlist = [
    // { 'id': 0, 'name': 'aaa', 'price': 23.5, 'num': 2, 'pic': "https://hshop.honorfile.com/pimages/cnqx/product/6…46D39B28CC8120465646880F48B18547F697D6D4266mp.png" }
]

const cart = (state = initcartlist, action) => {
    const { type, data } = action
    switch (type) {
        case ADD_CART:
            const { id } = data
            const index = state.findIndex(value => value.id === id)
            if (index === -1) {//新增

                //虚拟数据
                let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
                let result = ''
                for (var i = 0; i < 24; i++) {
                    result += arr[Math.floor(Math.random() * 16)]
                }
                data.cardId = result//结束
                return [...state, data];
            } else {
                state[index].num++;
                return [...state]
            }
        case EDIT_CART: return [...data]

        default: return state;
    }
}
export default cart

// const { type, data } = action
// switch (type) {
//     case ADD_CART:
//         const { id } = data;
//         const index = state.findIndex(value => value.id === id)
//         if (index === -1) {
//             const arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c']
//         }